//analog in 5 = WHITE !

const int p_R = 3;
const int p_G = 9;
const int p_B = 6;
//const int p_Rmon = 6;
//const int p_Gmon = 10;
//const int p_Bmon = 11;
//button of the hue pot
//const int p_btnHue = 2;  ????? le digital 2 semble poser un souci au spectrum 
//button of the strobe pot
const int p_btnStrobe = 7;
//button controlling the preview led
//const int p_btnMon = 7;
//3 pos button controlling Off / On / Mic
const int p_btnOff = 8;
const int p_btnMic = 12;
const int p_btnWhite = 13;

//For spectrum analyzer shield, these three pins are used.
//You can move pinds 4 and 5, but you must cut the trace on the shield and re-route from the 2 jumpers. 
int spectrumReset=5;
int spectrumStrobe=4;
int spectrumAnalog=0;  //0 for left channel, 1 for right.

//hue potentiometer
const int hueIn = 2;
//stroboscop potentiometer
const int strobeIn = 3;

///############### CHEAT WHITE
int hsv_color[] = {1, 100, 100};
byte rgb_color[] = {0, 0, 0};

bool ledIsOff = true;
bool monIsOff = false;
bool strobeMode = false;
bool micMode = false;
bool whiteMode = true;

void setup(){
 // analogReference(INTERNAL);
 Serial.begin(115200);
 
  //Setup pins to drive the spectrum analyzer. 
 pinMode(spectrumReset, OUTPUT);
 pinMode(spectrumStrobe, OUTPUT);

  //Init spectrum analyzer
  digitalWrite(spectrumStrobe,LOW);
    delay(1);
  digitalWrite(spectrumReset,HIGH);
    delay(1);
  digitalWrite(spectrumStrobe,HIGH);
    delay(1);
  digitalWrite(spectrumStrobe,LOW);
    delay(1);
  digitalWrite(spectrumReset,LOW);
    delay(5);
 // Reading the analyzer now will read the lowest frequency.
 
 //Setting LED outputs
 
 pinMode(p_R, OUTPUT); 
 pinMode(p_G, OUTPUT); 
 pinMode(p_B, OUTPUT);
 
 //pinMode(p_Rmon, OUTPUT); 
 //pinMode(p_Gmon, OUTPUT); 
 //pinMode(p_Bmon, OUTPUT); 
 
 //Setting Inputs
 
 //pinMode(p_btnHue, INPUT); 
 pinMode(p_btnStrobe, INPUT); 
 //pinMode(p_btnMon, INPUT); 
 pinMode(p_btnOff, INPUT); 
 pinMode(p_btnMic, INPUT);
 pinMode(p_btnWhite, INPUT);
 
 //activating pullup resistors
 
 //digitalWrite(p_btnHue, HIGH);
 digitalWrite(p_btnStrobe, HIGH);
 //digitalWrite(p_btnMon, HIGH);
 digitalWrite(p_btnOff, HIGH);
 digitalWrite(p_btnMic, HIGH);
 digitalWrite(p_btnWhite, HIGH);
}

int t = 0;

void loop(){
  int hue = analogRead(hueIn);
  //hue = 0;
  int strobe = analogRead(strobeIn);  
  
  //on traite d'abord l'état des boutons
  ledIsOff = digitalRead(p_btnOff);
  //monIsOff = !digitalRead(p_btnMon);
  micMode = !digitalRead(p_btnMic);
  whiteMode = !digitalRead(p_btnWhite);
  strobeMode = !digitalRead(p_btnStrobe);
  
  hsv_color[0] = 0;
  hsv_color[1] = 100;
  hsv_color[2] = 100;
  
  //############# CHEAT A VIRER !  
  //micMode = true;
  //strobeMode = false;
  //ledIsOff = false;
  

  //Serial.print("valeur lue : ");
  //
  //Serial.println(hue,DEC);
  //calcul de la couleur courante
  if (hue > 660.0){
   hsv_color[1] = 0;
    hsv_color[2] = 100;
  } else {
  hue = (float)hue * 360.0 / 674.0;
  }
  
  /*if (whiteMode){
    hsv_color[1] = 0;
  } else {
    hsv_color[1] = 100;
  }*/
  hsv_color[0] = hue;
  hsv2Rgb(hsv_color, rgb_color);
    
    if(micMode){
      
      int mic_color[] = {0, 0, 0};
      if ( !strobeMode){
              calculateAudioMode(hsv_color, mic_color, 0);
      } else {
        calculateAudioMode(hsv_color, mic_color, max(0.05,(float)strobe / 674.0));
      }  
      hsv2Rgb(mic_color, rgb_color);
      
    } else if (strobeMode){
      
      int strobe_color[] = {0, 0, 0};
      
      int strobe20;
      float strobefreq = (1.0 - (float)floor(strobe) / 674.0);
      //round this to avoid shaking of the strobe value;
      strobe20 = strobefreq * 30;
      strobefreq = (float)strobe20 / 30.0;
      
      calculateStrobeMode(hsv_color, strobe_color, strobefreq);
      hsv2Rgb(strobe_color, rgb_color);
      
    } else {
      
      hsv2Rgb(hsv_color, rgb_color);
      
    }
  
  //mise a jour des circuits PWM
  
  if (!ledIsOff || micMode){    
    analogWrite(p_R, rgb_color[0]);
    analogWrite(p_G, rgb_color[1]);
    analogWrite(p_B, rgb_color[2]);
    //Serial.println("");
    //Serial.println(rgb_color[0], DEC);
    //Serial.println(rgb_color[1], DEC);
    //Serial.println(rgb_color[2], DEC);
  } else {
    analogWrite(p_R, 0);
    analogWrite(p_G, 0);
    analogWrite(p_B, 0);
  }
  
  /*if (!monIsOff){    
    analogWrite(p_Rmon, rgb_color[0]);
    analogWrite(p_Gmon, rgb_color[1]);
    analogWrite(p_Bmon, rgb_color[2]);  
  } else {
    analogWrite(p_Rmon, 0);
    analogWrite(p_Gmon, 0);
    analogWrite(p_Bmon, 0);
  }*/
}
